<template>
    <div class="minW1060-auto bg-fff mt10">
        <div class="pad20 ml10">
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/department' }">部门管理</el-breadcrumb-item>
                <el-breadcrumb-item>部门编辑</el-breadcrumb-item>
            </el-breadcrumb>
            <el-row>

                <el-col align="right">
                    <div class="dlblock-mb20 pr10 ml20">
                        <el-button type="primary" @click="updateDepartmentFn('department')">保存</el-button>
                    </div>
                    <div class="dlblock-mb20 pr10">
                        <el-button type="danger" @click="resetForm('department')">重置</el-button>
                    </div>
                </el-col>
            </el-row>

            <el-row>
                <el-col align="left">
                    <h1>基本信息</h1>
                </el-col>
            </el-row>

            <el-row :gutter="30" class="mt20">
                <el-form label-position="left" label-width="80px" :model="department"
                         ref="department">
                    <el-row>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="部门名称" prop="deptName" :rules="required">
                                <el-input v-model.trim="department.deptName" placeholder="请输入内容" :maxlength="20" show-word-limit
                                          clearable></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="部门拼音" prop="pinyin" :rules="required">
                                <el-input v-model.trim="department.pinyin" placeholder="请输入内容" :maxlength="20" show-word-limit
                                          clearable></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="拼音缩写" prop="abbr" :rules="required">
                                <el-input v-model.trim="department.abbr" placeholder="请输入内容" :maxlength="20" show-word-limit
                                          clearable></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="分组" prop="group" :rules="required">
                                <el-cascader
                                        v-model="department.group"
                                        :options="groupTree"
                                        :props="{value: 'id', label: 'name', checkStrictly: true,expandTrigger:'hover'}"
                                        style="width: 343px;"
                                        :show-all-levels="false"
                                        clearable
                                        placeholder="请选择">
                                </el-cascader>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row style="margin-left: 10px">
                        <el-col align="left">
                            <h1>联系信息</h1>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="负责人" prop="empId" :rules="required">
                                <el-select v-model="department.empId" filterable placeholder="请选择" clearable
                                           style="width: 343px">
                                    <el-option v-for="(item,index) in epmInfo"
                                               :key="index"
                                               :label="item.name"
                                               :value="item.empId">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="办公电话" prop="tel">
                                <el-input type="number" v-model.trim="department.tel" placeholder="请输入内容" :max="10"
                                          clearable></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="办公地址" prop="address">
                                <el-input v-model.trim="department.address" placeholder="请输入内容" :maxlength="20" show-word-limit
                                          clearable></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row style="margin-left: 10px">
                        <el-col align="left">
                            <h1>部门成员</h1>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-form-item prop="empId">
                            <el-transfer v-if="people" style="margin-left: 200px"
                                         v-model="selectedStaff"
                                         :data="epmInfo"
                                         :titles="['可选', '已选']"
                                         filterable
                                         filter-placeholder="请输入要素名称"
                                         :props="{key: 'empId',label: 'name'}">
                            </el-transfer>
                        </el-form-item>
                    </el-row>

                </el-form>

            </el-row>
        </div>
    </div>
</template>

<script>
    import {
        updateDepartment, getEpmInfo, getDeptEpmInfo, getGroupTree
    } from '@/api'
    import {} from '@/utils/config'
    import vPinyin from "../../../../utils/vue-py";

    export default {
        name: "index1",
        data() {
            return {
                group: '',
                groupTree: [],
                department: {
                    deptId: '',
                    deptName: '',
                    groupId: '',
                    group: '',
                    groupName: '',
                    tel: '',
                    peoPleName: '',
                    address: '',
                    pinyin: '',
                    abbr: '',
                    disabled: '',
                    empId: '',
                    empIds: [],
                    flags: '',
                },
                index: '',
                people: false,
                epmInfo: [],
                selectedStaff: [],
                required: {
                    required: true,
                    message: '此输入框必填',
                    trigger: 'blur'
                },
            };
        },
        created() {
            if (this.$route.query.department) {
                this.department = this.$route.query.department
                if (this.department.abbr == '' && this.department.abbr == null) {
                    this.department.abbr = this.department.deptName
                }
            }
            this.department.group = this.department.groupId
            this.getEpmInfoFn();
            this.getGroupTreeFn();
        },
        watch: {

            'department.deptName': {
                handler(newValue, oldValue) {
                    if (this.department.flags != 1) {
                        this.department.pinyin = vPinyin.chineseToPinYin(this.department.deptName)
                        let SX = '';
                        for (var i = 0; i < this.department.pinyin.length; i++) {
                            var c = this.department.pinyin.charAt(i);
                            if (/^[A-Z]+$/.test(c)) {
                                SX += c;
                            }
                        }
                        this.department.abbr = SX;
                    }
                }
            },

        },
        methods: {
            //添加或修改部门信息
            updateDepartmentFn(formName) {
                var groupId = this.department.group.length ? this.department.group[this.department.group.length - 1] : ''
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        updateDepartment({
                            deptId: this.department.deptId,
                            name: this.department.deptName,
                            pinyin: this.department.pinyin,
                            abbr: this.department.abbr,
                            disabled: this.department.disabled,
                            empId: this.department.empId,
                            tel: this.department.tel,
                            address: this.department.address,
                            empIds: this.selectedStaff,
                            groupId: groupId,
                        }).then(res => {
                            if (res.code == 200) {
                                this.$message.success("保存成功")
                                this.$router.push("/department")
                                this.$store.dispatch('tagsView/delView', {path: '/department/edit'})
                            }
                        }).catch(err => {
                            this.$message.error("保存失败")
                        })
                    } else {
                        return false;
                    }
                });
            },
            //查询人员
            getEpmInfoFn() {
                getDeptEpmInfo({
                    deptId: this.department.deptId
                }).then(res => {
                    if (res.code == 200) {
                        this.selectedStaff = res.data
                    }
                }),
                    getEpmInfo({
                        /*groupId:this.department.groupId,
                        deptId:this.department.deptId*/
                    }).then(res => {
                        if (res.code == 200) {
                            this.epmInfo = res.data
                            this.people = true
                        }
                    })
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            getGroupTreeFn() {
                getGroupTree({}).then(res => {
                    if (res.code == 200) {
                        this.groupTree = res.data.children
                    }
                })
            },
        }
    }
</script>

<style scoped>

</style>
